package org.bsplus.rpc.impl;

import org.bsplus.domain.Issue;
import org.bsplus.rpc.dto.IssueListItemDTO;
import org.bsplus.rpc.dto.IssueSeverityDTO;
import org.bsplus.rpc.dto.IssueTypeDTO;
import org.bsplus.util.MappingUtils;
import org.bsplus.rpc.dto.IssueDTO;
import org.bsplus.service.IssueService;
import java.util.List;
import javax.annotation.Resource;
import org.bsplus.domain.IssueSeverity;
import org.bsplus.domain.IssueType;
import org.bsplus.rpc.IssueRPCService;
import org.bsplus.util.pagination.PageLoadRequest;
import org.bsplus.util.pagination.ResultPage;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class IssueRPCServiceImpl implements IssueRPCService
{
    private IssueService issueService;

    private MappingUtils mappingUtils;

    @Resource
    @Required
    public void setPropertyService(IssueService issueService)
    {
        this.issueService = issueService;
    }

    @Resource
    @Required
    public void setMappingUtils(MappingUtils mappingUtils)
    {
        this.mappingUtils = mappingUtils;
    }

    @Override
    public List<IssueDTO> getAllIssues()
    {
        List<Issue> issues = issueService.getAllIssues();

        return mappingUtils.map(issues, IssueDTO.class);
    }

    @Override
    public List<IssueTypeDTO> getAllIssueTypes()
    {
        List<IssueType> issueTypes = issueService.getAllIssueTypes();

        return mappingUtils.map(issueTypes, IssueTypeDTO.class);
    }

    @Override
    public List<IssueSeverityDTO> getAllIssueSeverities()
    {
        List<IssueSeverity> issueSeverities = issueService.getAllIssueSeverities();

        return mappingUtils.map(issueSeverities, IssueSeverityDTO.class);
    }

    @Override
    public String createIssue(IssueDTO issueDTO)
    {
        Issue issue = mappingUtils.map(issueDTO, Issue.class);

        issueService.createIssue(issue);

        return issue.getDisplayIssueNumber();
    }

    @Override
    public ResultPage<IssueListItemDTO> listIssues(PageLoadRequest pageLoadRequest)
    {
        ResultPage<Issue> results = issueService.getIssues(pageLoadRequest);

        return mappingUtils.map(results, IssueListItemDTO.class);
    }
}
